/*
 *  Created on: Jul 12, 2012
 *      Author: Guannan Ma
 *      @mythmgn
 *
 *      Note:
 *      	Some functions are platform-dependent, such as getTotalPhyMemory(),
 *      	Be careful of the macro that you have to define before you use them.
 */

#include "base.h"

#include <string.h>

#if defined __linux__
#include <unistd.h>
#include <pwd.h>
#include <sys/types.h>

long getTotalPhyMemory() {
	long pages = sysconf(_SC_PHYS_PAGES);
	ASSERT(pages>0);
	long page_size = sysconf(_SC_PAGE_SIZE);
	ASSERT(page_size>0);
	return pages * page_size;
}
long getAvailPhyMem(){
	long pages= sysconf(_SC_AVPHYS_PAGES);
	long page_size = sysconf(_SC_PAGE_SIZE);

	ASSERT(pages>0);
	ASSERT(page_size>0);

	return pages * page_size;
}
long getUsedPhyMem(){
	long usedMem = getTotalPhyMemory()-getAvailPhyMem();
	ASSERT(usedMem>0);
	return usedMem;
}

unsigned int getPhyMemInfo(PhyMemInfo *pMemInfo){
	pMemInfo->total=getTotalPhyMemory();
	pMemInfo->free=getAvailPhyMem();
	pMemInfo->used=pMemInfo->total-pMemInfo->free;
	return TRUE;
}
BOOL isRunUnderRoot(){
	if(geteuid() != 0){
		return FALSE;
	}else{
		return TRUE;
	}
}

const struct passwd * getUserPW(const char* strUName){
    const struct passwd *user;
    while( (user = getpwent()) != 0){
        if( strcmp( user->pw_name, strUName) == 0){
            break;
        }
    }
    endpwent();
    return user;
}
#endif

#if defined WIN32
#include <windows.h>
long getTotalSystemMemory()
{
    MEMORYSTATUSEX status;
    status.dwLength = sizeof(status);
    GlobalMemoryStatusEx(&status);
    return status.ullTotalPhys;
}
#endif
